package com.kenyi.co.ui.classification;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.kenyi.co.R;
import com.kenyi.co.okhttp.OkHttpUtils;
import com.kenyi.co.okhttp.callback.StringCallback;
import com.kenyi.co.ui.classification.adapter.ThemeMainAdapter;
import com.kenyi.co.ui.classification.bean.CategoryBean;
import com.kenyi.co.ui.classification.bean.CategoryMainBean;
import com.kenyi.co.ui.home.ui.SearchActivity;
import com.kenyi.co.utils.NetConfig;
import com.kenyi.co.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 分类页面
 */
public class ClassificationFragment extends Fragment implements ThemeMainAdapter.OnSelectorListener, View.OnClickListener {

    private RecyclerView recyclerView;
    private ThemeMainAdapter adapter;
    private int pos=0;

    private ClassificationRightFragment themeFragment;
    private List<CategoryMainBean.DatasBean> categoryMainBean;

    public ClassificationFragment() {

    }

    public static ClassificationFragment getInstance() {
        return new ClassificationFragment();
    }

    private View view;
    private TextView tv_word;
    private RelativeLayout re_fenlei_search;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        super.onCreateView(inflater, container, savedInstanceState);
        view = inflater.inflate(R.layout.fragment_classification, container, false);
        initview();
        getItem();
        initListener();
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    private void initview() {
        recyclerView = view.findViewById(R.id.recyclerview);

        //初始化recyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext(), LinearLayoutManager.VERTICAL, false));
//        recyclerView.addItemDecoration(new SpaceItemDecoration(getActivity().getApplicationContext(), 5, 3));

        //初始化adapter
        adapter = new ThemeMainAdapter(getActivity());
        adapter.setOnSelectorListener(this);
        re_fenlei_search = view.findViewById(R.id.re_fenlei_search);
        re_fenlei_search.setOnClickListener(this);
    }


    private void dealWithData(List<CategoryMainBean.DatasBean> datasBeanList) {
        //传递数据
        adapter.setData(datasBeanList);
        recyclerView.setAdapter(adapter);
        //默认选中
        datasBeanList.get(0).setChick(true);
        //创建Fragment对象
        themeFragment = new ClassificationRightFragment();
        FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, themeFragment);

        //传递数据到Fragment
        Bundle mBundle = new Bundle();
        mBundle.putSerializable("info", datasBeanList.get(0));
        themeFragment.setArguments(mBundle);
        fragmentTransaction.commit();
    }

    private void initListener() {

    }

    @Override
    public void onSelect(View view, int position) {

        //选中处理
        CategoryMainBean.DatasBean datasBean = categoryMainBean.get(position);
        for (int i = 0; i < categoryMainBean.size(); i++) {
            if (categoryMainBean.get(i).getShowName().equals(datasBean.getShowName())) {
                categoryMainBean.get(i).setChick(true);
            } else {
                categoryMainBean.get(i).setChick(false);
            }
        }

        //刷新列表
        adapter.notifyDataSetChanged();

        //创建Fragment对象
        themeFragment = new ClassificationRightFragment();
        FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, themeFragment);

        //传递数据到Fragment
        Bundle mBundle = new Bundle();
        if(pos==position){
            return;
        }
        mBundle.putSerializable("info", categoryMainBean.get(position));
        themeFragment.setArguments(mBundle);
        fragmentTransaction.commit();
        pos=position;
    }

    /**
     * 分类页的左分类类别
     */
    public void getItem() {

        Map<String, String> params = new HashMap<>();

        params.put("version", "2");
        Map<String, String> headers = new HashMap<>();
        headers.put("APP-Key", "APP-Secret");

        String url = NetConfig.CATEGORY_ITEM;
        OkHttpUtils.get()
                .url(url)
                .params(params)
                .headers(headers)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {


                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        CategoryBean packlist = gson.fromJson(response, CategoryBean.class);
                        int code = packlist.getCode();
                        if (code == 200) {
                            categoryMainBean = new ArrayList<CategoryMainBean.DatasBean>();
                            List<String> datas = packlist.getDatas();
                            if (datas != null && datas.size() > 0) {
                                for (int i = 0; i < datas.size(); i++) {
                                    CategoryMainBean.DatasBean datasBeans = new CategoryMainBean.DatasBean();
                                    datasBeans.setId(i + "");
                                    datasBeans.setShowName(datas.get(i));
                                    categoryMainBean.add(datasBeans);
                                }

                            }

                            dealWithData(categoryMainBean);
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.re_fenlei_search:
                Intent mecontact = new Intent(getActivity(), SearchActivity.class);
                getActivity().startActivity(mecontact);
                break;

        }

    }
}

